﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Utility.Win32;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace System.Utility.Helper
{
    // Token: 0x02000048 RID: 72
    public static class Images
    {
        // Token: 0x0600034C RID: 844 RVA: 0x0000F914 File Offset: 0x0000DB14
        public static Image CreateThumbnail(Stream fileStream, int width, int height, System.Drawing.Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(fileStream);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return Images.CreateThumbnail(bitmap, width, height, penColor);
        }

        // Token: 0x0600034D RID: 845 RVA: 0x0000F958 File Offset: 0x0000DB58
        public static Image CreateThumbnail(string fileName, int width, int height, System.Drawing.Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(fileName);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return Images.CreateThumbnail(bitmap, width, height, penColor);
        }

        // Token: 0x0600034E RID: 846 RVA: 0x0000F99C File Offset: 0x0000DB9C
        public static Image CreateThumbnail(Image image, int width, int height, System.Drawing.Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(image);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return Images.CreateThumbnail(bitmap, width, height, penColor);
        }

        // Token: 0x0600034F RID: 847 RVA: 0x0000F9E0 File Offset: 0x0000DBE0
        public static Image CreateThumbnail(Bitmap bitmap, int width, int height, System.Drawing.Color penColor)
        {
            width = ((bitmap.Width > width) ? width : bitmap.Width);
            height = ((bitmap.Height > height) ? height : bitmap.Height);
            Bitmap bitmap2 = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
            Image result;
            using (Graphics graphics = Graphics.FromImage(bitmap2))
            {
                int num = width;
                int num2 = height;
                if (bitmap.Width > bitmap.Height)
                {
                    num2 = (int)((float)bitmap.Height / (float)bitmap.Width * (float)num);
                }
                else if (bitmap.Width < bitmap.Height)
                {
                    num = (int)((float)bitmap.Width / (float)bitmap.Height * (float)num2);
                }
                int x = width / 2 - num / 2;
                int y = height / 2 - num2 / 2;
                graphics.PixelOffsetMode = PixelOffsetMode.None;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(bitmap, x, y, num, num2);
                using (System.Drawing.Pen pen = new System.Drawing.Pen(penColor, 1f))
                {
                    graphics.DrawRectangle(pen, 0, 0, bitmap2.Width - 1, bitmap2.Height - 1);
                }
                result = bitmap2;
            }
            return result;
        }

        // Token: 0x06000350 RID: 848 RVA: 0x0000FB34 File Offset: 0x0000DD34
        public static void DrawImage(Graphics g, Rectangle rect, Image img, float opacity)
        {
            if (opacity > 0f)
            {
                using (ImageAttributes imageAttributes = new ImageAttributes())
                {
                    Images.SetImageOpacity(imageAttributes, (opacity >= 1f) ? 1f : opacity);
                    Rectangle rectangle = new Rectangle(rect.X, rect.Y + rect.Height / 2 - img.Size.Height / 2, img.Size.Width, img.Size.Height);
                    g.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttributes);
                }
            }
        }

        // Token: 0x06000351 RID: 849 RVA: 0x0000FC00 File Offset: 0x0000DE00
        public static void SetImageOpacity(ImageAttributes imgAttributes, float opacity)
        {
            float[][] array = new float[5][];
            float[][] array2 = array;
            int num = 0;
            float[] array3 = new float[5];
            array3[0] = 1f;
            array2[num] = array3;
            float[][] array4 = array;
            int num2 = 1;
            array3 = new float[5];
            array3[1] = 1f;
            array4[num2] = array3;
            float[][] array5 = array;
            int num3 = 2;
            array3 = new float[5];
            array3[2] = 1f;
            array5[num3] = array3;
            float[][] array6 = array;
            int num4 = 3;
            array3 = new float[5];
            array3[3] = opacity;
            array6[num4] = array3;
            array[4] = new float[]
            {
                0f,
                0f,
                0f,
                0f,
                1f
            };
            float[][] newColorMatrix = array;
            ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
            imgAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        }

        // Token: 0x06000352 RID: 850 RVA: 0x0000FC84 File Offset: 0x0000DE84
        public static ImageSource CreateImageSourceThumbnia(string fileName, double width, double height)
        {
            Image image = Image.FromFile(fileName);
            double val = width / (double)image.Width;
            double val2 = height / (double)image.Height;
            float num = (float)Math.Min(val, val2);
            int width2 = image.Width;
            int height2 = image.Height;
            if (num < 1f)
            {
                width2 = (int)Math.Round((double)((float)image.Width * num));
                height2 = (int)Math.Round((double)((float)image.Height * num));
            }
            Bitmap bitmap = new Bitmap(image, width2, height2);
            IntPtr hbitmap = bitmap.GetHbitmap();
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            bitmapSource.Freeze();
            Win32.Win32.DeleteObject(hbitmap);
            image.Dispose();
            bitmap.Dispose();
            return bitmapSource;
        }

        // Token: 0x06000353 RID: 851 RVA: 0x0000FD54 File Offset: 0x0000DF54
        public static ImageSource CreateImageSourceThumbnia(Image sourceImage, double width, double height)
        {
            ImageSource result;
            if (sourceImage == null)
            {
                result = null;
            }
            else
            {
                double val = width / (double)sourceImage.Width;
                double val2 = height / (double)sourceImage.Height;
                float num = (float)Math.Min(val, val2);
                int width2 = sourceImage.Width;
                int height2 = sourceImage.Height;
                if (num < 1f)
                {
                    width2 = (int)Math.Round((double)((float)sourceImage.Width * num));
                    height2 = (int)Math.Round((double)((float)sourceImage.Height * num));
                }
                Bitmap bitmap = new Bitmap(sourceImage, width2, height2);
                IntPtr hbitmap = bitmap.GetHbitmap();
                BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                bitmapSource.Freeze();
                Win32.Win32.DeleteObject(hbitmap);
                sourceImage.Dispose();
                bitmap.Dispose();
                result = bitmapSource;
            }
            return result;
        }

        // Token: 0x06000354 RID: 852 RVA: 0x0000FE2C File Offset: 0x0000E02C
        public static ImageSource CreateImageSourceThumbnia(byte[] data, double width, double height)
        {
            ImageSource result;
            using (Stream stream = new MemoryStream(data, true))
            {
                using (Image image = Image.FromStream(stream))
                {
                    result = Images.CreateImageSourceThumbnia(image, width, height);
                }
            }
            return result;
        }

        // Token: 0x06000355 RID: 853 RVA: 0x0000FE94 File Offset: 0x0000E094
        public static ImageSource CreateImageSourceFromImage(Bitmap image)
        {
            ImageSource result;
            if (image == null)
            {
                result = null;
            }
            else
            {
                try
                {
                    IntPtr hbitmap = image.GetHbitmap();
                    BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    bitmapSource.Freeze();
                    image.Dispose();
                    Win32.Win32.DeleteObject(hbitmap);
                    result = bitmapSource;
                }
                catch (Exception)
                {
                    result = null;
                }
            }
            return result;
        }
    }
}
